<?php
namespace app\api\controller;
use app\common\Tool\ApiErrDesc;
use app\common\BaseController;
use think\Log;

/**
 * 套餐
 */
class Commodity extends BaseController
{

	private $table1 = 'commodity';
    private $table1_view = 'view_commodity';
    private $table2 = 'commodity_orders';
	private $table_view = '';
	public function _initialize(){
        $this->noNeedLogin = [];
    }

   /**
     * 查询商品表信息
     *
     * @Author GNLeon 
     * @DateTime 2020年8月17日11:32:17
     * @param [type] $request
     *
     * @return void
     */
	public function selectCommodity(){

		$offset = $this->request->param('offset');
        $length = $this->request->param('length')?:10;
        $Keyword = $this->request->param('keyword');
        $status = $this->request->param('status')?:'0';//1上架 2下架
        $type = $this->request->param('type'); //1个人 2公司
        // $search[0] = $Keyword?'businessName like "%'.$Keyword.'%"':'';
        $search[0] = '';
        $search[1] = $status?'status = '.$status:'status = '.$status;
        // $search[2] = 'userid = -1';
        $search[2] = $type==1?'type = 1':($type == 2?'type = 2':'');

        $where = '';

        foreach ($search as $key => $value) {
            # code...
            if($value){
                $where=$where.$value.' and ';
            }
        }

        $where=substr($where,0,strlen($where)-5);

        $order = 'id desc';

        // if($this->user->roleid == 2 ){
        //     if($where){
        //         $where = $where.' and type = 2';
        //     }else{
        //         $where = 'type = 2';
        //     }
        // }else{
        //     if($where){
        //         $where = $where.' and type = 1';
        //     }else{
        //         $where = 'type = 1';
        //     }
        // }
        
		$data=$this->select($this->table1_view,$where,'*',$offset,$length,$order);	

		$total=$this->total($this->table1_view,$where);
		return $this->jsonSuccessData($data,$total);
	}

    /**
     * 添加/修改套餐
     * @HtttpRequest                          get|post
     * @Author       GNLEON
     * @Param
     * @DateTime     2020-08-18T08:53:59+0800
     * @LastTime     2020-08-18T08:53:59+0800
     */
    public function runcommodity(){
         $out_trade_no = $this->request->param('out_trade_no')?:'';
         if($out_trade_no!=''){
            Log::info('微信支付信息：'.$out_trade_no.',支付用户:'.$this->user['openid']);
            $where =  'tradeNo = '.$out_trade_no;
            $data = [ 
                'expritime' => time()+(30*24*3600),
                'payTime' => time(),
                'status' => 2, 
            ];
            $this->update($this->table2, $where, $data);
            return $this->jsonSuccessData();
         }

        $id = $this->request->param('id');
        $where =  'id = '.$id;

        $dataArray = array(
            'title' => $this->request->param('title'),
            'userid' => $this->uid,
            'money' => $this->request->param('price'),
            'outnumber' => $this->request->param('outnumber')?:'NULL',
            'outAppnumber' => $this->request->param('outAppnumber')?:'NULL',
            'year' => $this->request->param('year')?:'NULL',
            'month' => $this->request->param('month')?:'NULL',
            'day' => $this->request->param('day')?:'NULL',
            'remark' => $this->request->param('remark')?:'NULL',
            'createtime' => time(),
            'status' => $this->request->param('status')?:0
        );


        if(empty($id)){
           try {
           $this->insert($this->table1,$dataArray);
        } catch (\Throwable $th) {
            exception(ApiErrDesc::ERR_PARAMS[1],ApiErrDesc::ERR_PARAMS[0]);
            }
        }else{
          try {
            $this->update($this->table1,$where,$dataArray);
        } catch (\Throwable $th) {
            exception(ApiErrDesc::ERR_PARAMS[1],ApiErrDesc::ERR_PARAMS[0]);
            }
        }

        return $this->jsonSuccessData();
    }



}